#install packages
library(panelView)
library(zoo)
library(StaggeredSynthDiD)
library(synthdid)
library(fect)
library(ggplot2)

#prepare data
laws = read.csv("statelaws.csv")
turnout = read.csv("stateturnout.csv")
laws = laws[laws$State!="",]
results <- list()

for (i in 1:nrow(laws)) {
  for (j in 2:ncol(laws)) { 
    if (!is.na(laws[i, j])) {
      if (laws[i, j] == "X") {
        results <- c(results, list(data.frame(STATE = laws$State[i], YEAR = colnames(laws)[j], Law = TRUE)))
      }else {
        results <- c(results, list(data.frame(STATE = laws$State[i], YEAR = colnames(laws)[j], Law = FALSE)))
      }
    }else {
      results <- c(results, list(data.frame(STATE = laws$State[i], YEAR = colnames(laws)[j], Law = FALSE)))
    }
  }
}
laws <- do.call(rbind, results)
laws$YEAR <-substring(laws$YEAR,2,10000)

data = merge(laws,turnout,by=c("STATE","YEAR"))
data = data[!(data$STATE %in%c("Texas")),]
data = data[data$YEAR != 1982&data$YEAR != 1980,]
data$Y = as.numeric(gsub(",","",data$VOTE_FOR_HIGHEST_OFFICE))/as.numeric(gsub(",","",data$VEP))*100

names(data)[names(data)=="Law"] <- "W"
names(data)[names(data)=="STATE"] <- "State"
names(data)[names(data)=="YEAR"] <- "Year"



data$W = as.numeric(data$W)

#create early vs late adopting groups
data$group = as.numeric(data$State %in% c("Arizona", "Georgia", "Indiana", "Ohio")) + 2*as.numeric(data$State %in% c("Alabama", "Kansas", "Mississippi", "North Dakota", "Tennessee", "Virginia", "Wisconsin"))

#synthetic difference-in-differences requires these column names
data$Unit = data$State
data$Time = data$Year

data  = data[data$State %in% c("California", "District of Columbia", "Illinois", "Iowa", "Maine", "Maryland", "Massachusetts", "Minnesota", "Nebraska", "Nevada", "New Jersey", "New Mexico", "New York", "Oregon", "Pennsylvania", "Vermont", "West Virginia", "Wyoming","Alabama","Georgia","Kansas","Mississippi","Tennessee","Wisconsin","Arizona","Ohio","Indiana","North Dakota", "Virginia"),]


all_sdid_rest = StaggeredSynthDiD(data[data$State != "Virginia" & data$State != "North Dakota",],vcov="jack")
presidential_sdid_rest = StaggeredSynthDiD(data[as.numeric(data$Year)%%4 == 0& data$State != "Virginia" & data$State != "North Dakota",],vcov="jack")
midterm_sdid_rest = StaggeredSynthDiD(data[as.numeric(data$Year)%%4 == 2,],vcov="jack")
all_early_sdid_rest <- StaggeredSynthDiD( data[ data$group != 2,],vcov="jack")
presidential_early_sdid_rest <- StaggeredSynthDiD( data[as.numeric(data$Year)%%4 == 0& data$group != 2,],vcov="jack")
midterm_early_sdid_rest <- StaggeredSynthDiD( data[as.numeric(data$Year)%%4 == 2& data$group != 2,],vcov="jack")
all_late_sdid_rest <- StaggeredSynthDiD( data[data$State != "Virginia" & data$State != "North Dakota" & data$group != 1,],vcov="jack")
presidential_late_sdid_rest <- StaggeredSynthDiD( data[as.numeric(data$Year)%%4 == 0& data$State != "Virginia" & data$State != "North Dakota" & data$group != 1,],vcov="jack")
midterm_late_sdid_rest <- StaggeredSynthDiD( data[as.numeric(data$Year)%%4 == 2& data$group != 1,],vcov="jack")

all_sdid_es_rest = StaggeredSynthDiD_es(data[data$State != "Virginia" & data$State != "North Dakota",],vcov="jack")
presidential_sdid_es_rest = StaggeredSynthDiD_es(data[as.numeric(data$Year)%%4 == 0& data$State != "Virginia" & data$State != "North Dakota",],vcov="jack")
midterm_sdid_es_rest = StaggeredSynthDiD_es(data[as.numeric(data$Year)%%4 == 2,],vcov="jack")
all_early_sdid_es_rest <- StaggeredSynthDiD_es( data[data$group != 2,],vcov="jack")
presidential_early_sdid_es_rest <- StaggeredSynthDiD_es( data[as.numeric(data$Year)%%4 == 0& data$group != 2,],vcov="jack")
midterm_early_sdid_es_rest <- StaggeredSynthDiD_es( data[as.numeric(data$Year)%%4 == 2& data$group != 2,],vcov="jack")
all_late_sdid_es_rest <- StaggeredSynthDiD_es( data[data$State != "Virginia" & data$State != "North Dakota" & data$group != 1,],vcov="jack")
presidential_late_sdid_es_rest <- StaggeredSynthDiD_es( data[as.numeric(data$Year)%%4 == 0& data$State != "Virginia" & data$State != "North Dakota" & data$group != 1,],vcov="jack")
midterm_late_sdid_es_rest <- StaggeredSynthDiD_es( data[as.numeric(data$Year)%%4 == 2& data$group != 1,],vcov="jack")
